/*
 * -------------------------------------------------------------------------------------------------------*
 * SBS Priceboard V2.0
 * Copyright (C) 2008 Unicom Vietnam JSC
 * -------------------------------------------------------------------------------------------------------* 
 * HeadURL: http://192.168.1.4/svn/priceboard/trunk/SBS_PriceboardV2/front-end/end-user/i_priceboard/
 * LastChangeBy:
 * LastChangeDate:
 */
package com.unicom.priceboard.data.hose;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.apache.log4j.Logger;

import com.unicom.priceboard.data.vo.LeElement;
import com.unicom.priceboard.thread.LeThread;
import com.unicom.priceboard.util.Util;

/**
 * Creation date: Jul 16, 2008
 * @author quan
 */
public class LeReader {
	
	
	private static final Logger logger = Logger.getLogger(LeReader.class);
	
	public void parseLe(String path){
		try{
			FileInputStream instrLe = new FileInputStream(Util.getCurrentDataPath(path));
			readDATFile(instrLe);	
			instrLe.close();
		}catch(Exception e){
			logger.debug("not read file: "+e.getMessage());
		}
	}
	
	private synchronized void readDATFile(InputStream is)
	{
		DataInputStream input = new DataInputStream(is);
		try
		{
			while(input.available()>0)
			{
				LeElement oLe = new LeElement();
				
				//Create a new ByteBuffer to store a record from file
				ByteBuffer buf = ByteBuffer.allocate(32);
				//Set to little endian
				buf.order(ByteOrder.LITTLE_ENDIAN);
				
				//Get each field from the file and put it to the byte buffer
				buf.putInt(input.readInt());//StockNo
				buf.putInt(input.readInt());//Price
				buf.putInt(input.readInt());//AccumulatedVol
				buf.putDouble(input.readDouble());//AccumulatedVal
				buf.putInt(input.readInt());//Highest
				buf.putInt(input.readInt());//Lowest
				buf.putInt(input.readInt());//Time
				
				//Reset position for reading
				buf.flip();
				// Set to big endian
				buf.order(ByteOrder.BIG_ENDIAN);
				
				//Retrieve data from byte buffer
				oLe.setStockNo(buf.getInt());
				oLe.setPrice(buf.getInt());
				oLe.setAccumulatedVol(buf.getInt());
				oLe.setAccumulatedVal(buf.getDouble());
				oLe.setHighest(buf.getInt());
				oLe.setLowest(buf.getInt());
				oLe.setTime(buf.getInt());
				
				//cache for Session 1
				//if(TimerThread.CURRENT_TIME<=Constants.LE_READ_END) {
					LeElement oLe1 = new LeElement();
					oLe1.setAccumulatedVol(oLe.getAccumulatedVol());
					oLe1.setPrice(oLe.getPrice());
					oLe1.setStockNo(oLe.getStockNo());
					oLe1.setTime(oLe.getTime());
					//
					if(LeThread.T1_lastest_object.containsKey((short)oLe.getStockNo())==false) {
						LeThread.T1_lastest_object.put((short)oLe1.getStockNo(), oLe1);
					}else {
						LeElement oInList = LeThread.T1_lastest_object.get((short)oLe.getStockNo());
						if(oLe.getTime()<=oInList.getTime()) {
							LeThread.T1_lastest_object.remove((short)oInList.getStockNo());
							LeThread.T1_lastest_object.put((short)oLe1.getStockNo(), oLe1);
						}
					}
			//	}
				
				//cache for Session 2
				LeElement oLe_inList = LeThread.T2_lastest_object.get((short)oLe.getStockNo());
				if(oLe_inList==null) {
					LeThread.T2_lastest_object.put((short)oLe.getStockNo(), oLe);
				}
				else {
					if(oLe.getTime()<=101500 && oLe.getTime()>=90000 && oLe.getTime()>oLe_inList.getTime())
					{
						oLe.setCurrentMatchVol(oLe.getAccumulatedVol()-oLe_inList.getAccumulatedVol());
						LeThread.T2_lastest_object.remove((short)oLe_inList.getStockNo());
						LeThread.T2_lastest_object.put((short)oLe.getStockNo(), oLe);
					}
				}
				
				String key = String.valueOf(oLe.getStockNo())+"_"+oLe.getTime();
				if(!LeThread.matchingList.containsKey(key))
					LeThread.matchingList.put(key, oLe);
			}
		}catch(Exception ex)
		{
			
		}
		
	}
}
